package cn.edu.scau.cmi.ema.controller;

import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.base.CmiEmaController;
import cn.edu.scau.cmi.ema.domain.Material;
import cn.edu.scau.cmi.ema.util.CmiHibernateProxyUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Set;

@Controller("MaterialController")
public class MaterialController extends CmiEmaController {

	@RequestMapping("/listMaterials")
	public ModelAndView listMaterials(HttpServletRequest request, @ModelAttribute CmiPagination pagination) {
		ModelAndView mav = new ModelAndView();

		pagination = materialPaginationService.updatePagination(request, pagination);
		Set<Material> materials = materialPaginationService.getPaginationMaterials(pagination);

		mav.addObject("materials", materials);
		mav.addObject("entityName", "material");
		mav.addObject("pagination", pagination);
		mav.setViewName("material/listMaterials");
		return mav;
	}

	public String listMaterials() {
		return "redirect:/listMaterials";
	}

	@RequestMapping("/viewMaterial")
	public ModelAndView viewMaterial(@RequestParam Integer materialId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("material", materialDAO.findMaterialByPrimaryKey(materialId));
		mav.setViewName("material/viewMaterial");
		return mav;
	}

	@RequestMapping(value = "/confirmDeleteMaterials")
	public ModelAndView confirmDeleteMaterials(
			@RequestParam(value = "deleteMaterialsIds", required = true) Set<Integer> deleteMaterialsIds) {
		ModelAndView mav = new ModelAndView();

		Set<Material> deleteMaterials = materialDAO.findMaterialsByIds(deleteMaterialsIds);
		Set<Material> referencedMaterials = materialService.loadReferenceMaterials(deleteMaterials);
		boolean hasReferenced;
		if (referencedMaterials.size() == 0) {
			mav.addObject("deleteEntitys", deleteMaterials);
			hasReferenced = false;
		} else {
			deleteMaterials.removeAll(referencedMaterials);
			mav.addObject("referencedEntitys", referencedMaterials);
			mav.addObject("deleteEntitys", deleteMaterials);
			hasReferenced = true;
		}
		mav.addObject("entityName", "Material");
		mav.addObject("hasReferenced", hasReferenced);
		mav.setViewName("forward:/listMaterials");
		return mav;
	}

	@RequestMapping("/deleteMaterial")
	public String deleteMaterial(@RequestParam Integer materialId) {
		Material material = materialDAO.findMaterialByPrimaryKey(materialId);
		materialService.deleteMaterial(material);
		return "forward:/listMaterials";
	}

	@RequestMapping("/confirmDeleteMaterial")
	public ModelAndView confirmDeleteMaterial(@RequestParam Integer materialId) {

		ModelAndView mav = new ModelAndView();

		mav.addObject("material", materialDAO.findMaterialByPrimaryKey(materialId));

		mav.setViewName("material/deleteMaterial");

		return mav;
	}

	@RequestMapping("/deleteMaterials")
	public String deleteMaterials(
			@RequestParam(value = "deleteMaterialsId", required = true) Set<Integer> deleteMaterialsId) {
		materialService.deleteMaterials(deleteMaterialsId);
		return "forward:/listMaterials";
	}

	@RequestMapping("/editMaterial")
	public ModelAndView editMaterial(@RequestParam Integer materialId) {

		ModelAndView mav = new ModelAndView();

		Material material = materialDAO.findMaterialByPrimaryKey(materialId);
		// 将需要编辑的对象加到mav对象中
		mav.addObject("material", material);

		// 把所有的引用material对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(materialService.loadMaterialObjectReferencedSet(material));

		// 将编辑页面material添加到mav中
		mav.setViewName("material/editMaterial");
		return mav;
	}

	@RequestMapping("/newMaterial")
	public ModelAndView newMaterial() {
		ModelAndView mav = new ModelAndView();
		Material material = new Material();

		mav.addObject("material", material);

		// 把所有的引用material对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(materialService.loadMaterialObjectReferencedSet(material));

		mav.addObject("newFlag", true);
		mav.setViewName("material/editMaterial");

		return mav;
	}

	@RequestMapping("/saveMaterial")
	public ModelAndView saveMaterial(@ModelAttribute Material material) {

		ModelAndView mav = new ModelAndView();

		Material existingMaterial = CmiHibernateProxyUtil
				.deproxy(materialDAO.findMaterialByPhysicalPrimaryKey(material.getName(), material.getQuality()));

		// 新建记录
		if (material.getId() == null) {
			if (material.equals(existingMaterial)) {

				mav.addAllObjects(materialService.loadMaterialClassReferencedSet(material));
				// mav = editContinue(mav, material);
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("material/editMaterial");
			} else {
				materialService.saveMaterial(material);
				mav.setViewName("forward:/listMaterials");
			}
		}
		// 编辑记录
		else {
			Material originMaterial = CmiHibernateProxyUtil
					.deproxy(materialDAO.findMaterialByPrimaryKey(material.getId()));
			if (!material.equals(originMaterial) && material.equals(existingMaterial)) {
				mav.addAllObjects(materialService.loadMaterialClassReferencedSet(material));
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("material/editMaterial");
			} else {
				materialService.saveMaterial(material);
				mav.setViewName("forward:/listMaterials");
			}
		}
		return mav;
	}

	@RequestMapping("/materialController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request,
			@ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

}
